package ctrl;

import java.io.IOException;

import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;

import util.Constants.EVEData;
import data.EVECentralMarketOrder;
import data.EVESystem;
import data.EVECentralMarketValues;
import data.EVEItem;

public class EVECentralXMLHandler implements ContentHandler {

  public EVECentralXMLHandler(EVEData data, EVEItem item, EVESystem system)
      throws IOException {
    _item = item;
    _system = system;
    _data = data;
    // if (data == EVEData.Systems) {
    // _item._addSystem(system);
    // }
  }

  @Override
  public void setDocumentLocator(Locator locator) {
  }

  @Override
  public void startDocument() throws SAXException {
  }

  @Override
  public void endDocument() throws SAXException {
  }

  @Override
  public void startPrefixMapping(String prefix, String uri) throws SAXException {
  }

  @Override
  public void endPrefixMapping(String prefix) throws SAXException {
  }

  @Override
  public void startElement(String uri, String localName, String qName,
      Attributes atts) throws SAXException {
    if (_isValidOrderName(localName)) {
      _currentOrder = localName;
      _enable = true;
    }
    else if (_isValidValueName(localName)) {
      _currentValue = localName;
    }
  }

  @Override
  public void endElement(String uri, String localName, String qName)
      throws SAXException {
    if (_isValidOrderName(localName)) {
      _currentOrder = "";
      _enable = false;
    }
    else if (_isValidValueName(localName)) {
      _currentValue = "";
    }
  }

  @Override
  public void characters(char[] ch, int start, int length) throws SAXException {
    if (_enable) {
      String value = "";
      for (int i = start; i < (start + length); i++) {
        value += ch[i];
      }
      _addValue(value);
    }
  }

  @Override
  public void ignorableWhitespace(char[] ch, int start, int length)
      throws SAXException {
  }

  @Override
  public void processingInstruction(String target, String data)
      throws SAXException {
  }

  @Override
  public void skippedEntity(String name) throws SAXException {
  }

  private boolean _isValidOrderName(String key) {
    switch (key) {
    case "buy":
    case "sell":
    case "all":
      return true;
    }
    return false;
  }

  private boolean _isValidValueName(String key) {
    switch (key) {
    case "volume":
    case "avg":
    case "max":
    case "min":
    case "stddev":
    case "median":
    case "percentile":
      return true;
    }
    return false;
  }

  private void _addValue(String value) {
    if(_data == EVEData.Items){
      EVECentralMarketOrder order = _item.getItemMarketDataMap().get(_system);
      if (_currentOrder.equals("buy")) {
        _addValueToOrder(value, order._buyValues);
      }
      else if (_currentOrder.equals("sell")) {
        _addValueToOrder(value, order._sellValues);
      }
      else if (_currentOrder.equals("all")) {
        _addValueToOrder(value, order._allValues);
      }
    }
    
    if(_data == EVEData.Systems){
      EVECentralMarketOrder order = _system.getSystemMarketDataMap().get(_item);
      if (_currentOrder.equals("buy")) {
        _addValueToOrder(value, order._buyValues);
      }
      else if (_currentOrder.equals("sell")) {
        _addValueToOrder(value, order._sellValues);
      }
      else if (_currentOrder.equals("all")) {
        _addValueToOrder(value, order._allValues);
      }
    }

  }

  private void _addValueToOrder(String value, EVECentralMarketValues values) {
    if (_currentValue.equals("volume")) {
      values._volume = value;
    }
    else if (_currentValue.equals("avg")) {
      values._avg = value;
    }
    else if (_currentValue.equals("max")) {
      values._max = value;
    }
    else if (_currentValue.equals("min")) {
      values._min = value;
    }
    else if (_currentValue.equals("stddev")) {
      values._stddev = value;
    }
    else if (_currentValue.equals("median")) {
      values._median = value;
    }
    else if (_currentValue.equals("percentile")) {
      values._percentile = value;
    }
  }

  private EVEItem   _item;
  private EVESystem _system;
  private String    _currentOrder;
  private String    _currentValue;
  private boolean   _enable;
  private EVEData   _data;
}

// <buy><volume>1287</volume><avg>7748188.53</avg><max>10806038.01</max><min>2400500.00</min>
// <stddev>2448663.66</stddev><median>8500000.01</median><percentile>10688420.73</percentile></buy>
